/*
 *更新 击倒玩家也生效.
 *特征 坦克拍打玩家，和石头击中玩家 发出咚咚响，和屏幕震动效果.
 */
 
#include <sourcemod>
#include <sdktools>

#define SOUND_BCLAW		"weapons/grenade_launcher/grenadefire/grenade_launcher_explode_1.wav"

#define PLUGIN_VERSION "1.0"

public Plugin:myinfo =
{
	name = "[L4D2]TANK攻击反馈",
	author = "ヾ藤野深月ゞ",
	description = "提取依据RPG中TANK攻击玩家时的受击反馈",
	version = PLUGIN_VERSION,
	url = "https://gitee.com/sweet-and-sour-bombs/optional-bank-attack-feedback"
}

public OnPluginStart()
{
	/* Event */
	HookEvent("player_hurt",			Event_PlayerHurt, EventHookMode_Pre);
	HookEvent("player_incapacitated",			Event_PlayerIncapacitated, EventHookMode_Pre);
}

public OnMapStart()
{
	/* 预载音效 */
	PrecacheSound(SOUND_BCLAW, true);
}

/* 玩家受伤 */
public Action:Event_PlayerHurt(Handle:event, String:event_name[], bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new String:WeaponUsed[256];
	GetEventString(event, "weapon", WeaponUsed, sizeof(WeaponUsed));
	if(IsValidPlayer(victim) && GetClientTeam(victim) == 2 && !IsFakeClient(victim))
	{
		if(StrEqual(WeaponUsed, "tank_claw") || StrEqual(WeaponUsed, "tank_rock"))
		{
			EmitSoundToAll(SOUND_BCLAW, victim);
			ScreenFade(victim, 200, 0, 0, 150, 80, 1);
			ScreenShake(victim, 50.0);
		}
	}
	return Plugin_Handled;
}

/* 玩家倒地 */
public Action:Event_PlayerIncapacitated(Handle:event, String:event_name[], bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new String:WeaponUsed[256];
	GetEventString(event, "weapon", WeaponUsed, sizeof(WeaponUsed));
	if(IsValidPlayer(victim) && GetClientTeam(victim) == 2 && !IsFakeClient(victim))
	{
		if(StrEqual(WeaponUsed, "tank_claw") || StrEqual(WeaponUsed, "tank_rock"))
		{
			EmitSoundToAll(SOUND_BCLAW, victim);
			ScreenFade(victim, 200, 0, 0, 150, 80, 1);
			ScreenShake(victim, 50.0);
		}
	}
	return Plugin_Handled;
}


stock bool:IsValidPlayer(Client)
{
	if (Client < 1 || Client > MaxClients) return false;
	if (!IsClientConnected(Client) || !IsClientInGame(Client)) return false;
	return true;
}

public ScreenFade(target, red, green, blue, alpha, duration, type)
{
	if(IsClientInGame(target)){
		new Handle:msg = StartMessageOne("Fade", target);
		BfWriteShort(msg, 500);
		BfWriteShort(msg, duration);
		if (type == 0)
			BfWriteShort(msg, (0x0002 | 0x0008));
		else
			BfWriteShort(msg, (0x0001 | 0x0010));
		BfWriteByte(msg, red);
		BfWriteByte(msg, green);
		BfWriteByte(msg, blue);
		BfWriteByte(msg, alpha);
		EndMessage();
	}
}

public ScreenShake(target, Float:intensity)
{
	new Handle:msg;
	msg = StartMessageOne("Shake", target);

	BfWriteByte(msg, 0);
 	BfWriteFloat(msg, intensity);
 	BfWriteFloat(msg, 10.0);
 	BfWriteFloat(msg, 3.0);
	EndMessage();
}